package flc.ast.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import flc.ast.R;
import flc.ast.databinding.FragmentTimeCalBinding;
import flc.ast.utils.DateAndTimeDialog;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.ToastUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TimeCalFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TimeCalFragment extends BaseNoModelFragment<FragmentTimeCalBinding> implements DateAndTimeDialog.TimePickerDialogInterface {

    private DateAndTimeDialog dateAndTimeDialog;
    private Date dateTime;

    @Override
    protected int onCreate() {
        return R.layout.fragment_time_cal;
    }

    @Override
    protected void initView() {
        Date currentDate = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd EE");
        dateTime = currentDate;
        mDataBinding.tvTimeCal.setText(dateFormat.format(currentDate));
        mDataBinding.tvTimeCalRet.setText(dateFormat.format(currentDate));
        mDataBinding.llTimeCal.setOnClickListener(this);
        mDataBinding.ivStartCal.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llTimeCal:
                dateAndTimeDialog = new DateAndTimeDialog(getContext(), R.id.llTimeCal);
                dateAndTimeDialog.showDatePickerDialog();
                break;
            case R.id.ivStartCal:
                calculate();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void calculate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd EE");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        if (mDataBinding.etTimeCalNum.getText().toString().isEmpty()){
            ToastUtils.showShort(getString(R.string.please_input_day));
            return;
        }
        int distance = -Integer.parseInt(mDataBinding.etTimeCalNum.getText().toString());
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+distance);
        Date retDate;
        mDataBinding.tvTimeCalRet.setText(dateFormat.format(calendar.getTime()));
    }

    public void myPositiveListener(int viewCode) throws ParseException {
        SimpleDateFormat stringFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = stringFormat.parse(dateAndTimeDialog.getDate());
        dateTime = date;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd EE");
        String time = dateFormat.format(date);
        mDataBinding.tvTimeCal.setText(time);
    }

    @Override
    public void positiveListener(int viewCode) {

    }

    @Override
    public void negativeListener() {

    }
}